#include <REGX52.H>
#include <INTRINS.H>
#include "Delay.h"
#include "Nixie.h"
#include "Key.h"
#include "Beep.h"
#include "Timer0.h"


sbit Beeper = P2^5;

unsigned char KeyNum = 0;
unsigned char FreqSelect = 0;


unsigned int code FreqTabble[] = {
	63776, 63877, 63967, 64058, 64139, 64218, 64293, 64362, 64427, 64490, 64548, 64604,
	64656, 64705, 64751, 64795, 64836, 64875, 64913, 64948, 64981, 65012, 65041, 65069,
	65095, 65119, 65143, 65165, 65186, 65205, 65223, 65241, 65258, 65273, 65288, 65302,
	0
};

//C调

//低音组
#define L1		0
#define L1_		1
#define L2		2
#define L2_		3
#define L3		4
#define L4		5
#define L4_		6
#define L5		7
#define L5_		8
#define L6		9
#define L6_		10
#define L7		11

////中音组
#define M1		12
#define M1_		13
#define M2		14
#define M2_		15
#define M3		16
#define M4		17
#define M4_		18
#define M5		19
#define M5_		20
#define M6		21
#define M6_		22
#define M7		23


//高音组
#define H1		24
#define H1_		25
#define H2		26
#define H2_		27
#define H3		28
#define H4		29
#define H4_		30
#define H5		31
#define H5_		32
#define H6		33
#define H6_		34
#define H7		35


#define  PERID 125


unsigned char code Music[] = {
	//第1小节，4拍
	M1, 4,
	M1, 4,
	M5, 4,
	M5, 4,
	
	//第2小节，4拍
	M6, 4,
	M6, 4,
	M5, 4+4,
	//M5,
	
	//第3小节，4拍
	M4, 4,
	M4, 4,
	M3, 4,
	M3, 4,
	
	//第4小节，4拍
	M2, 4,
	M2, 4,
	M1, 4+4,
	//M1,
	
	//第5小节，4拍
	M5, 4,
	M5, 4,
	M4, 4,
	M4, 4,
	
	//第6小节，4拍
	M3, 4,
	M3, 4,
	M2, 4+4,
	//M2,
	
	
	//第7小节，4拍
	M5, 4,
	M5, 4,
	M4, 4,
	M4, 4,
	
	//第8小节，4拍
	M3, 4,
	M3, 4,
	M2, 4+4,
	
	//第9小节，4拍
	M1, 4,
	M1, 4,
	M5, 4,
	M5, 4,
	
	//第10小节，4拍
	M6, 4,
	M6, 4,
	M5, 4+4,
	
	//第11小节，4拍
	M6, 4,
	M6, 4,
	M3, 4,
	M3, 4,
	
	M2, 4,
	M2, 4,
	M1, 4+4,
	
	//结束
	0
};

void main()
{
	P0 = 0x0;
	Beeper = 0;
	
	Timer0_Init();
	
	while(1)
	{
		if(Music[FreqSelect]){
			TR0 = 1;
			Delay(PERID*Music[FreqSelect+1]);
			FreqSelect++;
			
			TR0 = 0;
			Delay(2);
			FreqSelect++;
		}
		else if(Music[FreqSelect] == 0){
			while(1);
		}
	}
}

/**
  * @brief  定时器0中断处理函数模版
  * @param  无
  * @retval 无
  */
void Timer_Routine(void) interrupt 1
{
	Beeper = !Beeper;
	
	//定时器溢出之后需要重新装载
	TH0 = FreqTabble[Music[FreqSelect]] / 256;				//12MHz晶振，12分频
	TL0 = FreqTabble[Music[FreqSelect]] % 256; 			//
}
